#!/bin/sh

if [ "${0##*/}" = "obsdman" ]; then
	OS=OpenBSD
	DOMAIN="http://www.openbsd.org/cgi-bin"
	MANPATH="OpenBSD+Current"
else
	OS=FreeBSD
	DOMAIN="http://www.freebsd.org/cgi"
	MANPATH="FreeBSD+10-current"
fi

usage () {
	cat <<EOF>&2
Usage: ${0##*/} [SECTION] PAGE

Fetch $OS man page PAGE from the official website and display it.

EOF
}

[ $# -eq 0 ] && usage && exit 1
[ "$1" = "-h" ] && usage && exit
[ "$1" = "--" ] && shift

SECTION=0
PAGE=$1
if [ $# -eq 2 ]; then
	SECTION="$1"
	PAGE="$2"
fi


AGENT="curl -s -L"
if ! command -v curl >/dev/null 2>&1; then
	AGENT="wget -q -O - "
fi

$AGENT "$DOMAIN/man.cgi?query=$PAGE&apropos=0&sektion=$SECTION&manpath=$MANPATH&arch=default&format=ascii" | if [ "$TERM" = dumb ]; then cat; else less; fi
